package com.sq.universe.system.service.impl;

import cn.hutool.core.io.FileUtil;
import com.sq.universe.base.service.impl.BaseServiceImpl;
import com.sq.universe.system.dao.FileDao;
import com.sq.universe.system.entity.FileEntity;
import com.sq.universe.system.entity.UserEntity;
import com.sq.universe.system.service.FileService;
import com.sq.universe.system.utils.FastDFSClientWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.zip.Inflater;

/**
* @author 孙乾 2021-01-14 22:34:24
*/
@Service
public class FileServiceImpl extends BaseServiceImpl<FileEntity, FileDao> implements FileService {

    @Autowired
    private FileDao fileDao;

    @Autowired
    private FastDFSClientWrapper fastDFSClientWrapper;

    /**
     * 文件上传
     * @param file
     * @param user
     * @param fcId
     * @return
     */
    public FileEntity upload(MultipartFile file, UserEntity user,Integer fcId){
        FileEntity fileEntity = new FileEntity();
        try {
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") );
            String filePath = fastDFSClientWrapper.uploadFile(file);
            fileEntity.setFileCreateTime(new Date());
            fileEntity.setFileCreateUser(user.getUserId());
            fileEntity.setFileFcId(fcId);
            fileEntity.setFileName(file.getOriginalFilename());
            fileEntity.setFilePath(filePath);
            fileEntity.setFileSource("用户上传");
            fileEntity.setFileSuffix(suffix);
            fileEntity.setFileSize(Double.valueOf(file.getSize()));
            fileDao.insert(fileEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
       return fileEntity;
    }

    public byte[] downLoad(FileEntity fileEntity){
        try {
            return fastDFSClientWrapper.downFile(fileEntity.getFilePath());
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 系统生成的问题
     * @param file
     * @param user
     * @param fcId
     * @return
     */
    public FileEntity uploadBySystem(File file, UserEntity user, Integer fcId){
        FileEntity fileEntity = new FileEntity();
        try {
            String suffix = file.getName().substring(file.getName().lastIndexOf(".") );
            String filePath = fastDFSClientWrapper.uploadFile(file);
            fileEntity.setFileCreateTime(new Date());
            fileEntity.setFileCreateUser(user.getUserId());
            fileEntity.setFileFcId(fcId);
            fileEntity.setFileName(file.getName());
            fileEntity.setFilePath(filePath);
            fileEntity.setFileSource("用户上传");
            fileEntity.setFileSuffix(suffix);
            fileEntity.setFileSize(Double.valueOf(FileUtil.size(file)));
            fileDao.insert(fileEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
        return fileEntity;
    }
}
